﻿/*
 * @file: BillingDA.cs
 * @in namespace: DAL
 */
using System;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for BillingDA
/// </summary>
namespace DAL
{
    public class BillingDA : BaseDAL, IBilling
    {
        private const string tableName = "Billing";
        private const string billID = "BillID";
        private const string userID = "UserID";
        private const string billDate = "BillDate";
        private const string totalAmount = "TotalAmount";

        public BillingDA()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IBilling Members

        /*
         * @method: GetBillingByCustomerID
         * @description: get billings by CustomerID
         * @input: CustomerID,
         * @output: records contain the corresponding CustomerID 
         */
        public Billing[] GetBillingByUserID(int _userID)
        {
            Billing[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                    cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                        tableName, userID, userID);
                    cmd.Parameters.Add(String.Format("@{0}", userID), SqlDbType.Int).Value = _userID;
                

                String[] columnNames = {    billID,
                                            userID,
                                            billDate,
                                            totalAmount};
                result = SelectCollection<Billing>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: GetBillingByBillID
         * @description: get a record by BillID from BillingTable
         * @input: BillID
         * @output: records contain the corresponding BillID
         */
        public Billing GetBillingByBillID(int _billID)
        {
            Billing[] result = null;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = String.Format("SELECT * FROM {0} WHERE {1} = @{2}",
                    tableName, billID, billID);
                cmd.Parameters.Add(String.Format("@{0}", billID), SqlDbType.Int).Value = _billID;


                String[] columnNames = {    billID,
                                            userID,
                                            billDate,
                                            totalAmount};
                result = SelectCollection<Billing>(columnNames, columnNames, cmd);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            if (result.Length > 0)
                return result[0];
            else
                return null;
        }

        /*
         * @method: InsertBilling
         * @description: insert new Billing record
         * @input: CustomerID, TotalAmount
         * @output: 1 if successful, 0 if error occurred
         */
        public int InsertBilling(Billing _billing, out int autoNumber)
        {
            int result = 0;
            try
            {
                String[] columnNames = {    userID,
                                            totalAmount};

                Object[] values = { _billing.UserID,
                                    _billing.TotalAmount};
                result = BaseDAL.InsertTableWithReturnID(tableName, columnNames, values, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        /*
         * @method: UpdateBilling
         * @description: update BillDate for BillingTable
         * @input: BillID, BillDate
         * @output: 1 if successful, 0 if error occurred
         */
        public int UpdateBilling(Billing _billing)
        {
            int result = 0;
            try
            {
                String[] columnNames = { billDate };
                Object[] values = { _billing.BillDate };
                String[] keyColumnNames = { billID };
                Object[] keyColumnValues = { _billing.BillID };
                result = UpdateTable(tableName, columnNames, values, keyColumnNames, keyColumnValues);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}